<template>
  <div class="preview-box">
    <div class="preview-content">
      <div class="preview-files" v-if="imgSrc">
        <el-image
          ref="preview"
          :src="imgSrc"
          :preview-src-list="[imgSrc]"
        >
        </el-image>
        <div class="preview-cover">
          <svg-icon icon-class="preview" @click="previewImage" class-name='custom-class'></svg-icon>
          <svg-icon icon-class="delete" @click="deleteImage" class-name='custom-class'></svg-icon>
        </div>
      </div>
      <div class="preview-btn" @click="previewAdd" v-else>
        <i class="el-icon-plus" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Index',
  props: {
    imgSrc: {
      type: [String],
      default: ''
    }
  },
  methods: {
    previewAdd() {
      this.$emit('previewAdd')
    },
    preview() {
      this.$refs.preview.clickHandler()
    },
    previewImage() {
      this.$emit('previewBtn')
    },
    deleteImage() {
      this.$emit('deleteImage')
    }
  }
}
</script>

<style lang="scss" scoped>
  .preview-box {
    .preview-content {
      .preview-files {
        display: inline-block;
        width: 58px;
        height: 58px;
        text-align: center;
        line-height: 58px;
        border: 1px solid #c0ccda;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 2px 2px 5px rgb(0 0 0 / 10%);
        margin-right: 4px;
        box-sizing: border-box;
        -moz-user-select:none; /*火狐*/
        -webkit-user-select:none; /*webkit浏览器*/
        -ms-user-select:none; /*IE10*/
        user-select:none;
        &.preview-files:hover {
          .preview-cover {
            opacity: 1;
          }
        }
        img {
          width: 100%;
          height: 100%;
          display: inline-block;
          vertical-align: top;
        }
        .preview-cover {
          position: absolute;
          left: 0;
          right: 0;
          top: 0;
          bottom: 0;
          opacity: 0;
          background: rgba(0, 0, 0, .6);
          color: #fff;
          display: flex;
          align-items: center;
          justify-content: center;
          transition: opacity .3s;
          svg {
            font-size: 17px;
            cursor: pointer;
            margin: 0 4px;
            vertical-align: middle;
          }
        }
      }
      .preview-btn {
        width: 58px;
        height: 58px;
        border: 1px dashed #c0ccda;
        background-color: #ffffff;
        border-radius: 4px;
        overflow: hidden;
        box-sizing: border-box;
        text-align: center;
        cursor: pointer;
        &.preview-btn:hover {
          box-shadow: 2px 2px 5px rgb(0 0 0 / 10%);
        }
        .el-icon-plus {
          line-height: 58px;
          font-size: 20px;
          vertical-align: middle;
          display: inline-block;
        }
      }
    }
  }
</style>
